import { reqGetCategory1List, reqGetCategory2List, reqGetCategory3List } from '@/api/product/categorySelector'

const state = {
  // 分类id
  category1Id: '',
  category2Id: '',
  category3Id: '',
  // 分类列表
  category1List: [],
  category2List: [],
  category3List: []
}

const actions = {
  async getCategory1List({ commit }) {
    const result = await reqGetCategory1List()
    if (result.code === 200) {
      commit('SAVE_CATEGORY1_LIST', result.data)
    }
  },
  async getCategory2List({ commit }, category1Id) {
    const result = await reqGetCategory2List(category1Id)
    if (result.code === 200) {
      commit('SAVE_CATEGORY2_LIST', { category2List: result.data, category1Id })
    }
  },
  async getCategory3List({ commit }, category2Id) {
    const result = await reqGetCategory3List(category2Id)
    if (result.code === 200) {
      commit('SAVE_CATEGORY3_LIST', { category3List: result.data, category2Id })
    }
  }
}

const mutations = {
  SAVE_CATEGORY1_LIST(state, payload) {
    state.category1List = payload
  },
  SAVE_CATEGORY2_LIST(state, payload) {
    state.category2List = payload.category2List
    state.category1Id = payload.category1Id
    // 清空category2Id,3Id
    state.category2Id = ''
    state.category3Id = ''
    state.category3List = []
  },
  SAVE_CATEGORY3_LIST(state, payload) {
    state.category3List = payload.category3List
    state.category2Id = payload.category2Id
    state.category3Id = ''
  },
  SETCATEGORY3ID(state, category3Id) {
    state.category3Id = category3Id
  },
  RESETINFO(state) {
    state.category1Id = ''
    state.category2List = []
    state.category3List = []
    state.category2Id = ''
    state.category3Id = ''
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
